<?php namespace Home\Controller;
use \Hdphp\Controller\Controller;
use Common\Model\Shop_detailed;
use Common\Model\Shop;
use Common\Model\Shop_attribute;
use Common\Model\Attribute;
use Common\Model\Shop_list;
class ContentController extends Controller{
	public function index(){
//		获取商品id
		$shop_id=Q('get.sid',0,'intval');
		$shopDetailedModel=new Shop_detailed;
		$shopDetailedata=$shopDetailedModel
						->join('shop','car_shop.sid','=','car_shop_detailed.shop_sid')
						->where("shop_sid={$shop_id}")->find();						
		$shopModel=new Shop;	
		$shopData=$shopModel->orderBy('sid','DESC')->limit(8)->get();
		View::with('shopData',$shopData);
		View::with('shopDetailedata',$shopDetailedata);
			$AttributeModel=new Attribute;
			$attribute_aid=$AttributeModel->where("is_recycle=1")->lists('aid');
			$str_aid=implode(',', $attribute_aid);		
			$Shop_attrModel=new Shop_attribute;
			$shop_attr=$Shop_attrModel->where("shop_id={$shop_id} AND attribute_aid IN ({$str_aid})")->get();
	//		把相同attribute_aid的属性放在了一起
			$sameTemp=[];
			foreach($shop_attr as $v){
				$sameTemp[$v['attribute_aid']][]=$v;
			}
			//组合名字和值
			$finalAttr=[];
			foreach($sameTemp as $attribute_aid=>$v){			
					$finalAttr[]=[
					'name'=>$AttributeModel->where("aid={$attribute_aid}")->pluck('aname'),
					'value'=>$v
				];			
			}
//			p($finalAttr);exit;
			View::with('finalAttr',$finalAttr);	
			
			View::make();
	}
	public function getAttr(){
	//			获得异步传过来用户选择属性的ID字符串
					$attrStr=Q('post.str');
					$attrStr=substr($attrStr,0,strlen($attrStr)-1);
	//			到货品列表找到这个组合ID
					$shop_listModel=new Shop_list;
					$listData=$shop_listModel->where("combine = '{$attrStr}'")->find();
					if($listData==NULL){
						$listData['inventory']=0;
					}
					echo json_encode($listData);exit;		
			}
	
}
